package com.wzt.lianfirecontrol.fragment;

import android.content.Context;
import android.os.Bundle;
import android.os.Message;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.wzt.lianfirecontrol.activity.BaseActivity;
import com.wzt.lianfirecontrol.dao.MyHandler;

import java.io.File;

public class BaseFragment extends Fragment {

    protected MyHandler handler;
    private boolean isLoaded = false;
    public BaseFragment mFragment;

    public boolean isLoaded() {
        return isLoaded;
    }

    public BaseActivity getMyActivity() {
        return (BaseActivity) getActivity();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        isLoaded = true;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        handler = new MyHandler(context) {

            @Override
            public void dealWithMsg(Message msg) {
                // TODO Auto-generated method stub
                dealWithMyMsgs(msg);
            }
        };
    }

    protected void dealWithMyMsgs(Message msg) {
    }

    @Override
    public void onPause() {
        super.onPause();
        if (null != handler) {
            handler.removeCallbacksAndMessages(null);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    public void skipToTop(){}

    public boolean isRefreshing(){
        return false;
    }


    /**
     * 更新数据
     */
    public void updateData(){}

    /**
     * 图片回传成功
     * @param photoWhat
     */
    public void imgSuccessAction(File file, String img, int photoWhat){}

    private String makeFragmentName(int viewId) {
        return String.valueOf(viewId);
    }

    public Fragment instantiateFragment(ViewGroup container, BaseFragment baseFragment, Bundle bundle) {
        if (baseFragment == null)
            return null;
        FragmentManager fm = getChildFragmentManager();
        FragmentTransaction curTransaction = fm.beginTransaction();//开启事物

        String name = makeFragmentName(container.getId());//根据制定好的规则查找对应Fragment对象
        Fragment fragment = fm.findFragmentByTag(name);
        if(curTransaction==null)
            return null;
        if (fragment != null) {
            curTransaction.attach(fragment);//链接
        } else {
             mFragment = baseFragment;
             fragment = baseFragment;
            curTransaction.add(container.getId(), fragment,name);//此时还没有数据传递，先添加到队列中
        }
        if(fragment instanceof BaseFragment ){//无论是根据规则查找到的还是新生成的Fragment,都要在获取对象之后传递数据
            if (null == bundle){
                bundle = new Bundle();
            }
            fragment.setArguments(bundle);//Fragment之间的通信方式之一：向Fragment传值只能用setArguments的方法，而非Activity传统的Intent
        }
        if(curTransaction != null) {
            curTransaction.commitAllowingStateLoss();//这里参照原码实现。如果觉得丢失状态没关系可以用这个，如果在意状态，并已经处理好状态逻辑，就可以用commit
            fm.executePendingTransactions();//立即执行
            setUserVisibleHint(true);//这里如果需要 对子级别Fragment的显示状态进行设置，可用setUserVisibleHint();</span>
        }
        return fragment;
    }

    public void destroyFragment(FragmentManager fm, Object object) {
        if (fm == null)
            return;
        FragmentTransaction curTransaction = fm.beginTransaction();
        curTransaction.detach((Fragment) object);//移除，该方法属于切掉Fragment之间的链接
    }
}
